package cn.ht.jywrwater.module.ht.controller.admin.statisticsdevicecount;

import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;
import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import cn.ht.jywrwater.module.ht.controller.admin.statisticsdevicecount.vo.*;
import cn.ht.jywrwater.module.ht.convert.statisticsdevicecount.StatisticsDeviceCountConvert;
import cn.ht.jywrwater.module.ht.dal.dataobject.statisticsdevicecount.StatisticsDeviceCountDO;
import cn.ht.jywrwater.module.ht.service.statisticsdevicecount.StatisticsDeviceCountService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "管理后台 - 设备在线统计")
@RestController
@RequestMapping("/ht/statistics-device-count")
@Validated
public class StatisticsDeviceCountController {

    @Resource
    private StatisticsDeviceCountService statisticsDeviceCountService;

    @PostMapping("/create")
    @Operation(summary = "创建设备在线统计")
    @PreAuthorize("@ss.hasPermission('ht:statistics-device-count:create')")
    public CommonResult<Long> createStatisticsDeviceCount(@Valid @RequestBody StatisticsDeviceCountCreateReqVO createReqVO) {
        return success(statisticsDeviceCountService.createStatisticsDeviceCount(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备在线统计")
    @PreAuthorize("@ss.hasPermission('ht:statistics-device-count:update')")
    public CommonResult<Boolean> updateStatisticsDeviceCount(@Valid @RequestBody StatisticsDeviceCountUpdateReqVO updateReqVO) {
        statisticsDeviceCountService.updateStatisticsDeviceCount(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备在线统计")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:statistics-device-count:delete')")
    public CommonResult<Boolean> deleteStatisticsDeviceCount(@RequestParam("id") Long id) {
        statisticsDeviceCountService.deleteStatisticsDeviceCount(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备在线统计")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:statistics-device-count:query')")
    public CommonResult<StatisticsDeviceCountRespVO> getStatisticsDeviceCount(@RequestParam("id") Long id) {
        StatisticsDeviceCountDO statisticsDeviceCount = statisticsDeviceCountService.getStatisticsDeviceCount(id);
        return success(StatisticsDeviceCountConvert.INSTANCE.convert(statisticsDeviceCount));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备在线统计列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:statistics-device-count:query')")
    public CommonResult<List<StatisticsDeviceCountRespVO>> getStatisticsDeviceCountList(@RequestParam("ids") Collection<Long> ids) {
        List<StatisticsDeviceCountDO> list = statisticsDeviceCountService.getStatisticsDeviceCountList(ids);
        return success(StatisticsDeviceCountConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备在线统计分页")
    @PreAuthorize("@ss.hasPermission('ht:statistics-device-count:query')")
    public CommonResult<PageResult<StatisticsDeviceCountRespVO>> getStatisticsDeviceCountPage(@Valid StatisticsDeviceCountPageReqVO pageVO) {
        PageResult<StatisticsDeviceCountDO> pageResult = statisticsDeviceCountService.getStatisticsDeviceCountPage(pageVO);
        return success(StatisticsDeviceCountConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备在线统计 Excel")
    @PreAuthorize("@ss.hasPermission('ht:statistics-device-count:export')")
    @OperateLog(type = EXPORT)
    public void exportStatisticsDeviceCountExcel(@Valid StatisticsDeviceCountExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<StatisticsDeviceCountDO> list = statisticsDeviceCountService.getStatisticsDeviceCountList(exportReqVO);
        // 导出 Excel
        List<StatisticsDeviceCountExcelVO> datas = StatisticsDeviceCountConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备在线统计.xls", "数据", StatisticsDeviceCountExcelVO.class, datas);
    }

}
